import java.sql.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class Search extends JFrame implements ActionListener {
    JLabel p1, p2, p3;
    JTextField t1;
    JButton btn1, btn2, btn3;
    JTextArea ta1;

    //构造函数，所有控件的设计
    Search() {
        this.setTitle("校园卡查询！");
        p1 = new JLabel("请输入账号：");
        p2 = new JLabel("信息如下：");
        p3 = new JLabel("是否进入其他系统？请选择：");


        t1 = new JTextField(10);
        ta1 = new JTextArea(100, 300);

        btn1 = new JButton("查询");
        btn2 = new JButton("修改密码");
        btn3 = new JButton("重新登录");

        t1.addActionListener(this);
        btn1.addActionListener(this);
        btn2.addActionListener(this);
        btn3.addActionListener(this);

        Container c = getContentPane();
        c.setLayout(null);

        c.add(p1);
        c.add(p2);
        c.add(p3);

        c.add(t1);
        c.add(ta1);

        c.add(btn1);
        c.add(btn2);
        c.add(btn3);

        p1.setBounds(100, 10, 100, 40);
        p2.setBounds(100, 50, 100, 40);
        p3.setBounds(100, 250, 250, 40);

        t1.setBounds(200, 10, 100, 30);
        ta1.setBounds(100, 80, 400, 100);

        btn1.setBounds(300, 10, 80, 30);
        btn2.setBounds(300, 250, 100, 30);
        btn3.setBounds(400, 250, 100, 30);

        addWindowListener(new WindowAdapter() {//点击窗口的“X”关闭
            @Override
            public void windowClosing(WindowEvent e1) {
                dispose();
                System.exit(0);
            }
        });
        this.setBounds(300, 50, 600, 400);//窗体位置，大小
        this.setVisible(true);//窗体可见
        this.setResizable(false);//不可改变大小
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Connection con;
        Statement sql;
        ResultSet rs;
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        } catch (ClassNotFoundException e1) {
            System.out.println(" " + e1);
        }
        if ("查询".equals(e.getActionCommand())) {
            try {
                con = DriverManager.getConnection("jdbc:odbc:campus-card", "", "");//连接数据库;
                sql = con.createStatement();
                String no;
                no = t1.getText();
                rs = sql.executeQuery("SELECT * FROM message WHERE sno ='" + no + "'");
                //输出信息
                while (rs.next()) {
                    String sno = rs.getString(1);
                    String sname = rs.getString(2);
                    String pwd = rs.getString(3);
                    String type = rs.getString(4);
                    Double money = Double.parseDouble(rs.getString(5));
                    ta1.setText("学号:" + sno + "  " + "姓名:" + sname + "  " + "身份:" + type + "  " + "余额:" + money);
                }
                con.close();
            } catch (SQLException e1) {
                System.out.println(e1);
            }
        } else if ("重新登录".equals(e.getActionCommand())) {
            new Load().setVisible(true);
            this.dispose();//关闭窗体
        } else if ("修改密码".equals(e.getActionCommand())) {
            new Change().setVisible(true);
            this.dispose();
        }
    }

    public static void main(String args[]) {
        new Search();
    }

}
